package cn.stylefeng.guns.modular.basic.service.impl;

import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicCode;
import cn.stylefeng.guns.modular.basic.mapper.BasicCodeMapper;
import cn.stylefeng.guns.modular.basic.model.params.BasicCodeParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicCodeResult;
import  cn.stylefeng.guns.modular.basic.service.BasicCodeService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 通用编码 服务实现类
 * </p>
 *
 * @author zhucx
 * @since 2022-12-23
 */
@Service
public class BasicCodeServiceImpl extends ServiceImpl<BasicCodeMapper, BasicCode> implements BasicCodeService {

    @Override
    public void add(BasicCodeParam param){
        BasicCode entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BasicCodeParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BasicCodeParam param){
        BasicCode oldEntity = getOldEntity(param);
        BasicCode newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BasicCodeResult findBySpec(BasicCodeParam param){
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public List<BasicCodeResult> findListBySpec(BasicCodeParam param){
        return this.baseMapper.findListBySpec(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(BasicCodeParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(BasicCodeParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BasicCode getOldEntity(BasicCodeParam param) {
        return this.getById(getKey(param));
    }

    private BasicCode getEntity(BasicCodeParam param) {
        BasicCode entity = new BasicCode();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

	@Override
	public List<ZTreeNode> tree(BasicCodeParam basicCodeParam) {
		return this.baseMapper.tree(basicCodeParam);
	}

	@Override
	public List<BasicCodeResult> likePids(Integer id) {
		return this.baseMapper.likePids(id);
	}

}
